package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zzedu.common.annotation.DataScope;
import com.zzedu.common.core.text.Convert;
import com.zzedu.common.enums.CommonEnums;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.domain.*;
import com.zzedu.dto.ZzClassesDTO;
import com.zzedu.mapper.ZzClassesMapper;
import com.zzedu.mapper.ZzZzStuClassesRMapper;
import com.zzedu.params.ZzClassesParams;
import com.zzedu.service.*;
import com.zzedu.vo.ZzClassesVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 班级管理Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-01-29
 */
@Service
public class ZzClassesServiceImpl extends ServiceImpl<ZzClassesMapper, ZzClasses> implements IZzClassesService {

    @Autowired
    IZzSchoolService zzSchoolService;


    @Autowired
    IZzCourseService zzCourseService;

    @Autowired
    IZzTeacherService teacherService;

    @Autowired
    IZzTimetableService zzTimetableService;

    @Autowired
    ZzZzStuClassesRMapper zzZzStuClassesRMapper;

    @Override
    public LambdaQueryWrapper<ZzClasses> buildQuery(ZzClassesDTO zzClasses) {
        LambdaQueryWrapper<ZzClasses> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzClasses.getName()), ZzClasses::getName, zzClasses.getName());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzClasses.getLevel()), ZzClasses::getLevel, zzClasses.getLevel());
        lambdaQueryWrapper.eq(null != zzClasses.getSchoolId(), ZzClasses::getSchoolId, zzClasses.getSchoolId());
        lambdaQueryWrapper.eq(null != zzClasses.getTeacherId(), ZzClasses::getTeacherId, zzClasses.getTeacherId());
        lambdaQueryWrapper.eq(null != zzClasses.getState(), ZzClasses::getState, zzClasses.getState());
        if (CollectionUtils.isNotEmpty(zzClasses.getSchoolIdArray())) {
            lambdaQueryWrapper.in(ZzClasses::getSchoolId, zzClasses.getSchoolIdArray());
        }
        lambdaQueryWrapper.orderByDesc(ZzClasses::getId);
        return lambdaQueryWrapper;
    }

    @DataScope(otherIdColumnName = "school_id")
    public <E extends IPage<ZzClasses>> E page(E page, Wrapper<ZzClasses> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    @Override
    public List<ZzClassesVO> convertVO(List<ZzClasses> zzClassesList) {
        return Optional.ofNullable(zzClassesList).orElse(Lists.newArrayList()).stream().map(zzClasses -> {
            ZzClassesVO zzClassesVO = new ZzClassesVO();
            BeanUtils.copyBeanProp(zzClassesVO, zzClasses);
            if (null != zzClasses.getSchoolId()) {
                ZzSchool zzSchool = this.zzSchoolService.getById(zzClasses.getSchoolId());
                if (null != zzSchool) {
                    zzClassesVO.setSchoolName(zzSchool.getSchoolName());
                }
            }
            if (null != zzClasses.getTeacherId()) {
                ZzTeacher zzTeacher = teacherService.getById(zzClasses.getTeacherId());
                if (null != zzTeacher) {
                    zzClassesVO.setTeacherName(zzTeacher.getName());
                }
            }
            return zzClassesVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ZzClasses> getByZzClassesByStudentId(Long stuId) {
        LambdaQueryWrapper<ZzClasses> zzClassesLambdaQueryWrapper = Wrappers.lambdaQuery(ZzClasses.class);
//        zzClassesLambdaQueryWrapper.eq(ZzClasses::getCourseId, courseId);
        zzClassesLambdaQueryWrapper.eq(ZzClasses::getState, CommonEnums.YES.getCode());
        zzClassesLambdaQueryWrapper.apply("id in(select class_id from zz_stu_classes_r where deleted=0 and stu_id={0})", stuId);
        return this.list(zzClassesLambdaQueryWrapper);
    }

    @Override
    @DataScope(otherIdColumnName = "school_id")
    public ZzClasses getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, readOnly = false)
    public boolean save(ZzClassesParams zzClassesParams) {
        ZzClasses zzClasses = new ZzClasses();
        BeanUtils.copyBeanProp(zzClasses, zzClassesParams);
        if(null!=zzClassesParams.getId()){
            return this.updateById(zzClasses);
        }
        return this.save(zzClasses);
    }
}